#!/bin/bash
rm tmm_id_list.txt
rm *_opm.pdb
o_lip="POPC"
r_lip="1";
p_cid=""
c_lig=""
c_pep=""
w_inh=0
s_path=0
s_cen=0.15
d_opt=1
np_n=`nproc`
n_num=$(($np_n/2))

while [ -n "$1" ]; do
case "$1" in
 -m_path) echo "-m_path option is the pathway of mp_builder, with value $2"
	 m_path=$2
	 shift 1
   ;;
 -s_path) echo "-m_path option is the pathway of schrodinger, '0' is default, with value $2"
	 s_path=$2
	 shift 1
   ;;
 -p_com) echo "-p_com option is the pdb file of the target complex, with value $2"
	 p_com=$2
	 shift 1
   ;;
 -p_seq) echo "-p_seq option is optional, it's the txt file of the target complex sequeces that have to cover or be longer than the target sequenuces ,  with value $2"
	 p_seq=$2
	 shift 1
   ;;
 -p_tpt) echo "-p_tpt option is optional, it's the template pdb file, for example the AF predicted structure, containing all domains you want to fix, with value $2"
	 p_tpt=$2
	 shift 1
   ;;
 -p_cid) echo "-p_cid option is optional, it's the chain ID of the domain you want to use, this option may be launched several times , with value $2"
	 p_cid=$p_cid" "$2
	 shift 1
   ;;
 -c_lig) echo "-c_lig option is optional, it's the pdb file of the ligand that binds properly in target complex pdb file with CONECT, this option may be launched several times , with value $2"
	 c_lig=$c_lig" "$2
	 shift 1
   ;;
 -c_pep) echo "-c_pep option is optional, it's the pdb file of the peptide  that binds properly in target complex pdb file with CONECT, this option may be launched several times , with value $2"
	 c_pep=$c_pep" "$2
	 shift 1
   ;;
 -p_tmm) echo "-p_tmm option is the chain ID of the transmembrane domain you want to use, with value $2"
	 p_tmm=$p_tmm" "$2
	 shift 1
   ;;
 -w_inh) echo "-w_inh option is optional, it's the pdb file of the waters in the transmembrace pocket or '1' means generated by this script or '0' means not need , '0' is default with value $2"
	 w_inh=$2
	 shift 1
   ;;
 -n_num) echo "-n_num option is threads num for energy minimizatio, the half of threads num of your PC is default, with value $2"
	 n_num=$2
	 shift 1
   ;;
 -o_lip) echo "-o_lip option is lipid types for membrane, POPC is default, with value $2"
	 o_lip=$2
	 shift 1
   ;;
 -r_lip) echo "-r_lip option is lipid ratio for membrane, 1 is default, with value $2"
	 r_lip=$2
	 shift 1
   ;;
 -s_cen) echo "-s_cen option is salt concentration, 0.15 is default, with value $2"
	 s_cen=$2
	 shift 1
   ;;
 -d_opt) echo "-r_lip option is lipid ratio for membrane, 1 is default, with value $2"
	 d_opt=$2
	 shift 1
   ;;
 -h) cat $m_path/scripts/sys_build/help.txt; exit 0
   ;;
  *) echo "Option $1 not recognized"
   ;;
esac
shift 1
done


#export PATH_MPBER=$m_path
cp $m_path/scripts/sys_build/1extr_seq.pl ./
perl 1extr_seq.pl $p_com
sleep 3s

cp $m_path/scripts/sys_build/pdb2fasta.pl  ./
cp $m_path/tools/clustalw2 ./
cp $m_path/tools/psfgen ./
cp -r $m_path/tools/toppar ./
cp $m_path/databases/pdball.pir ./

chmod 777 clustalw2
chmod 777 psfgen

cp $m_path/scripts/sys_build/2extr_chainpdb_v3.pl ./
ids_inpdb="$( bash <<EOF
perl 2extr_chainpdb_v3.pl  $p_com  $p_cid 
EOF
)"
echo "chain IDs are $ids_inpdb"
sleep 3s

cp $m_path/scripts/sys_build/3add_template_pdball_v2.pl ./
perl 3add_template_pdball_v2.pl $p_tpt
sleep 3s

p_cid="$ids_inpdb"

crd_dir=`pwd`
for cin_id in ${ids_inpdb[@]};
do
        echo $cin_id
	chain_dir=$crd_dir"/chain_"$cin_id	
	cd $chain_dir
	pwd
	cp $m_path/scripts/homo_build/1make_target_fasta_v5.pl ./
	cp ../$p_seq ./
	perl 1make_target_fasta_v5.pl -ci $cin_id -sf $p_seq
	sleep 3s

	cp $m_path/scripts/homo_build/2re_nature_v4.pl ./
	perl 2re_nature_v4.pl chain_"$cin_id"
	sleep 3s

	cp $m_path/scripts/homo_build/3make_target_fasta_v4.pl ./
	perl 3make_target_fasta_v4.pl -ci $cin_id -sf $p_seq
	sleep 3s

	cp $m_path/scripts/homo_build/4fas2ali.pl ./
	perl 4fas2ali.pl chain_"$cin_id"_fe.fasta
	sleep 3s

	cp $m_path/scripts/homo_build/5build_profile.py  ./
	python3 5build_profile.py chain_"$cin_id"_fe
	sleep 3s

	cp $m_path/scripts/homo_build/6rank_template_v2.pl ./
	perl 6rank_template_v2.pl ../$p_com  $cin_id $p_tmm  $m_path
	sleep 3s

	cp $m_path/scripts/homo_build/7salign.py ./
	file2=$(cat 6rank_template_output.txt)
	input_for5=" "
	for line2 in $file2
	do
		input_for5=$input_for5" "$line2
	done
	input_for5=$input_for5" chain_"$cin_id" "$cin_id
	echo "input string for script5 is $input_for5"
	#for example: input_for5 = 1gdd A chain_B B
	sleep 3s
	python3 7salign.py $input_for5
	sleep 3s

	cp $m_path/scripts/homo_build/8remake_mulali_v2.pl ./
	perl 8remake_mulali_v2.pl $input_for5
	sleep 3s

	cp $m_path/scripts/homo_build/9align2d_mult.py ./
	python3 9align2d_mult.py chain_"$cin_id"_fe
	sleep 3s

	cp $m_path/scripts/homo_build/10remake_mulali_v2.pl ./
	perl 10remake_mulali_v2.pl $input_for5
	sleep 3s

	#declare -a template_id
	#template_id=()
	#file2=$(cat 5rank_template_output_forss.txt)
	#for line2 in $file2
	#do
	#	template_id+=($line2)
	#done
	#echo "input string of template_id is ${template_id[@]}"
	#for example: template_id = 5dhh B 4ej4 A

	cp $m_path/scripts/homo_build/11make_ssbond_info_v2.pl ./
	perl 11make_ssbond_info_v2.pl ../$p_com $cin_id 
	sleep 3s

	cp $m_path/scripts/homo_build/12model_mult.py ./
        python3 12model_mult.py chain_"$cin_id"_fe ssbond_filter_model_"$cin_id".txt 5	
	sleep 3s

	cp $m_path/scripts/homo_build/13check_model_index_v2.pl ./
	perl 13check_model_index_v2.pl  chain_"$cin_id"
	sleep 3s

	cp $m_path/scripts/homo_build/14align_select_model.pl ./
	perl 14align_select_model.pl ../$p_com  chain_"$cin_id"_fe 
	sleep 3s

	cp $m_path/scripts/homo_build/15graft_v5.pl ./
	perl 15graft_v5.pl  chain_"$cin_id"  $s_path  $n_num
	sleep 3s

	cp $m_path/scripts/homo_build/16loop_refine_v2.pl ./
	cp $m_path/scripts/homo_build/16loop_refine.py ./
	perl 16loop_refine_v2.pl  ../$p_com chain_"$cin_id" $cin_id $s_path
	sleep 3s
done


cd $crd_dir

cp $m_path/scripts/sys_build/4align_to_opm_v4.pl ./
perl 4align_to_opm_v4.pl -p $p_com -tm  $p_tmm  -ci $p_cid -lg $c_lig -lp  $c_pep -mp $m_path
sleep 3s

cp $m_path/scripts/sys_build/5change_chain_label.pl ./
perl 5change_chain_label.pl $p_cid
sleep 3s

cp $m_path/scripts/sys_build/6renumber_v2.pl ./
perl 6renumber_v2.pl $p_cid
sleep 3s

cp $m_path/scripts/sys_build/7add_ss_resave_v6.pl ./
perl 7add_ss_resave_v6.pl complex_align_label_renumber $s_path $p_cid
sleep 3s

cp $m_path/scripts/sys_build/8hstate_option_amber_v2.pl ./
perl 8hstate_option_amber_v2.pl complex_align_label_renumber_ssinfo $s_path
sleep 3s

cp $m_path/scripts/sys_build/9hstate_pdb_amber_v2.pl ./
perl 9hstate_pdb_amber_v2.pl complex_align_label_renumber_ssinfo  $p_cid
sleep 3s

#cp $m_path/scripts/sys_build/10add_pep_hetatm.pl ./
#perl 10add_pep_hetatm.pl   complex_prep_hs.pdb
#sleep 3s

 #packmol-memgen --pdb complex_prep_hs.pdb --lipids POPC:CHL1 --ratio 9:1 --preoriented --salt --salt_c Na+ --saltcon 0.15 --dist 12 --dist_wat 15 --overwrite  
 #packmol-memgen --pdb complex_prep_hs.pdb --lipids POPC:POPS//POPE --ratio 7:3//1 --preoriented --salt --salt_c Na+ --saltcon 0.15 --dist 12 --dist_wat 15 --overwrite  
 packmol-memgen --pdb complex_prep_hs.pdb --lipids $o_lip --ratio $r_lip --preoriented --salt --salt_c Na+ --saltcon $s_cen --dist 12 --dist_wat 15 --overwrite  
 #packmol-memgen --pdb chain_A_fe_model_align.pdb --lipids POPC:POPS//POPE --ratio 7:3//1 --preoriented --salt --salt_c Na+ --saltcon 0.15 --dist 12 --dist_wat 15 --overwrite  --minimize 



#if [[ "$w_inh" -ne 0 ]] 2>/dev/null  || [[ "$w_inh" != "0" ]]; then
#        if [[ "$w_inh" -eq 1  ]]; then
#                echo "generate waters in the pocket"
#
#        else
#                echo "user provides the pdb file of waters in the pocket"
#        fi
#else
#        echo "user does not want waters in the pocket"
#fi

cp $m_path/tools/cSPCE_kh.xvv  ./
cp $m_path/scripts/sys_build/10amber_water_probe_v3.pl ./
perl 10amber_water_probe_v3.pl  $p_tmm $w_inh
sleep 3s

cp $m_path/scripts/sys_build/11find_water_inhole_v2.pl ./
perl 11find_water_inhole_v2.pl  $p_tmm $w_inh
sleep 3s

cp $m_path/scripts/sys_build/12align_wat_rec_v2.pl ./
perl 12align_wat_rec_v2.pl  $p_tmm $w_inh
sleep 3s

cp $m_path/scripts/sys_build/13wat_inhole_del_2.pl ./
perl 13wat_inhole_del_2.pl  $w_inh
sleep 3s

cp $m_path/scripts/sys_build/14prepare_build_v8.pl ./
perl 14prepare_build_v8.pl -ci $p_cid -wt $w_inh  -lg $c_lig -lp  $c_pep  -p $p_com -at $m_path
sleep 3s

cp $m_path/scripts/sys_build/15prepare_run.pl ./
perl 15prepare_run.pl 
sleep 3s

cp $m_path/scripts/sys_build/16psfgen_build_v2.pl ./
perl 16psfgen_build_v2.pl -ci $p_cid -lg $c_lig -lp  $c_pep   -at $m_path
sleep 3s

cp $m_path/scripts/sys_build/17prep_namd_run.pl ./
perl 17prep_namd_run.pl  -at $m_path
sleep 5s

if [ $d_opt -eq 1 ]; then
        echo "remove temp files...\n";
	rm -rf chain_* *.pl aligned_prep_?.pdb  complex_align*  psfgen* *fasta pdball* bilayer* lig_psf* prot_psf* solv* prep* memb* for*txt *.log *.in ZT*.pdb *xvv toppar *pml *inp clustalw2 14ssinfo*.txt 4lig_pep_list.txt 7ssinfo_*txt bak*pdb box_size.txt  tmm_id_list.txt
fi

